其他
HTML5 + Javascript 写出一个钢琴
首发:GitHubClub
原文:juejin.im/post/6879708939190009869
学生时代的我们如果有像郎朗一样的琴技,想必追起女生会非常的容易,但在以前,一架钢琴对普通家庭来说,消费还蛮高的,所以我们不如自己写一架属于自己的钢琴,哈哈,虽然音效不如真的钢琴,但是能写出来,还挺有成就感的。
<ul>
<li><div></div><p></p></li>
<li><div></div><p></p></li>
<li><div></div></li>
<li><div></div><p></p></li>
<li><div></div><p></p></li>
<li><div></div><p></p></li>
<li><div></div></li>
<li><div></div></li>
</ul>
/*-------钢琴部分--------*/
ul{
width:480px;
height:360px;
transform: translate(-50%, -50%);
position: absolute;
top: 50%;
left: 50%;
}
li{
float:left;
list-style-type: none;
position: relative;
}
li>div{
height: 360px;
width: 60px;
background: rgba(255,255,255,.3);
border: 1px solid;
border-color:rgba(0, 0, 0, .8);
border-bottom-left-radius : 8px;
border-bottom-right-radius: 8px;
box-sizing: border-box;
box-shadow: /*inset 3px 0 10px #c9c6c6,*/
inset 5px -8px 0 #00000023;
text-align: center;
display:table-cell;
vertical-align: bottom;
}
li:not(:last-child)>div{
border-right: none;
}
li>div:hover{
background: rgb(255, 97, 97);
}
/* 当白键按下时的样式 */
.white_active{
box-shadow: inset 3px -2px 0 #00000036;
background: linear-gradient(45deg, rgba(255,255,255,.7), rgba(255,255,255,.5));
}
li>p{
height: 200px;
width: 40px;
background-color: #000;
border-bottom-left-radius : 5.5px;
border-bottom-right-radius: 5.5px;
box-shadow: inset 5px -7px 0 #2c2c2c;
position: absolute;
top:0;
left: 40px;
z-index: 999;
}
li>p:hover{
background: linear-gradient(45deg, #4c4c4c, #444444);
}
/* 当黑键按下时的样式 */
.black_active{
box-shadow:inset 3px -5px 0 #2c2c2c;
}
// 给钢琴添加音频
for (var i = 1; i <= 8; i++) {
var addaudio = document.createElement("audio");
addaudio.setAttribute("src", "audios/" + "w" + i + ".ogv");
document.body.appendChild(addaudio);
}
for (var i = 1; i <= 5; i++) {
var addaudio = document.createElement("audio");
addaudio.setAttribute("src", "audios/" + "b" + i + ".ogv");
document.body.appendChild(addaudio);
}
let audios = document.getElementsByTagName("audio"),
buttons = document.querySelectorAll("ul>li>div"),
blacks = document.querySelectorAll("ul>li>p");
for (var i = 0; i < 8; i++) {
buttons[i].index = i;
buttons[i].onmousedown = function () {
//alert(this.index);
buttons[this.index].classList.add('white_active')
audios[this.index].load();
audios[this.index].play();
};
buttons[i].onmouseup = function () {
buttons[this.index].classList.remove('white_active')
};
}
for (var i = 0; i < 5; i++) {
blacks[i].index = i + 8;
blacks[i].onmousedown = function () {
//alert(this.index);
blacks[this.index-8].classList.add('black_active')
audios[this.index].load();
audios[this.index].play();
//alert(audios[this.index].src);
};
blacks[i].onmouseup = function () {
blacks[this.index-8].classList.remove('black_active')
};
}
// 操作键盘钢琴白键发出声音
var keyCodes = new Array(83, 68, 70, 71, 72, 74, 75, 76, 69, 82, 89, 85, 73);
document.body.onkeydown = function (e) {
for (var i = 0; i < keyCodes.length; i++) {
if (e.keyCode == keyCodes[i]) {
if (i < 8) {
buttons[i].classList.add('white_active');
} else {
blacks[i-8].classList.add('black_active');
}
audios[i].load();
audios[i].play();
}
}
};
// 操作键盘钢琴黑键发出声音
document.body.onkeyup = function (e) {
//document.title=e.keyCode;
for (var i = 0; i < keyCodes.length; i++) {
if (e.keyCode == keyCodes[i]) {
if (i < 8) {
buttons[i].classList.remove('white_active');
} else {
blacks[i-8].classList.remove('black_active');
}
}
}
};
<div>
请输入简谱:
<input type="text" id="song" placeholder="请输入歌曲名">
<button id="check">确定</button>
</div>
然后开始写逻辑部分,scroe 对象是用来存放歌曲信息的,不想去网上找歌曲信息的,可以直接复制最后面部分的简谱。格式类似于 {‘歌曲名’:‘歌词和简谱’}
,这里可以用模板字符串的形式。
var scroe = {};
var numReg = /[1-9]/g;
var wordReg = /[\u4e00-\u9fa5]+/g;
var str;
var shows;
var words;
var wordsIndex;
var wordsCurrent;
var current;
function play(){
buttons.forEach((el)=>el.classList.remove('show'))
var songName = song.value;
if(songName === ''){
return alert('请输入歌曲名')
}
if(scroe.hasOwnProperty(songName)){
str = scroe[songName];
shows = str.match(numReg);
words = str.match(wordReg);
wordsIndex = 0;
wordsCurrent = 0;
current = 0;
console.log(songName)
buttons[shows[0]/1 - 1].classList.add('show');
text.textContent = words[0]
}else{
return alert('您输入的歌曲名尚未加入曲库')
}
}
check.addEventListener('click',play)
function start(){
// 显示歌词的部分
if(wordsCurrent>=words[wordsIndex].length-1){
wordsCurrent = 0;
wordsIndex++;
text.textContent = words[wordsIndex]
}else{
wordsCurrent++
}
if(current === shows.length-1){
buttons[shows[shows.length-1]/1 - 1].classList.remove('show');
return console.log('演奏结束',current)
}
var showButton = shows[current+1]/1 - 1;
var hiddenButton = shows[current]/1 - 1;
buttons[hiddenButton].classList.remove('show');
buttons[showButton].classList.add('show');
current++
}
然后再在对应的按键事件和鼠标点击事件加上这段代码。
i == shows[current]-1 ? start() : '';
现在我们想要的效果基本已经实现。
// 给琴键加上数字
buttons.forEach((el,index)=>el.textContent = index+1)
window.addEventListener('keydown',function(e){
if(e.keyCode == 86){
if(buttons[0].textContent != ''){
buttons.forEach((el)=>el.textContent = '')
}else{
buttons.forEach((el,index)=>el.textContent = index+1)
}
}
})
let x = document.getElementById("btn-js"),
y = document.getElementById("js"),
//介绍的隐藏与显示
function hide() {
y.style.display === "block" ? y.style.display = "none" : y.style.display = "block"
}
x.addEventListener('click',hide);